
#
# 启动虚拟环境命令 注意这里命令的路径
# source /Users/dev2025/Desktop/PyTools/PyToolsVenv/bin/activate
# 执行命令
# python3 /Users/dev2025/Desktop/PyTools/LanguageTools.py


import pandas as pd
import os
from datetime import datetime

# 配置变量
file_path = 'language.xls'
sheet_names = ['ME', 'CHATS', 'DISCOVER', 'ROOME', '登录']
skip_rows = 1

language_map = {
    'Chinese': '中文',
    'English': '英文',
    'Arabic': '阿拉伯语'
}

def generate_single_language_file(language_column, language_suffix):
    filename = f"ALL_{language_suffix}.json"
    
    if os.path.exists(filename):
        try:
            os.remove(filename)
            print(f"已清理旧文件: {filename}")
        except Exception as e:
            print(f"删除文件 {filename} 失败: {e}")
            return
    
    with open(filename, 'w', encoding='utf-8') as f:
        for sheet in sheet_names:
            try:
                df = pd.read_excel(
                    file_path,
                    sheet_name=sheet,
                    skiprows=skip_rows,
                    engine='xlrd',
                    usecols='A:D'
                )
                df.columns = ['Chinese', 'English', 'Arabic', 'key']
                df = df.fillna('').astype(str)
                
                sheet_lower = sheet.lower()
                
                start_separator = [
                    f'"{sheet_lower}_start_separator1": "==================================================================",',
                    f'"{sheet_lower}_start_separator2": "                                                                  ",',
                    f'"{sheet_lower}_start_separator3": "               {sheet_lower}_开始                     ",',
                    f'"{sheet_lower}_start_separator4": "                                                                  ",',
                    f'"{sheet_lower}_start_separator5": "==================================================================",'
                ]
                f.write('\n'.join(start_separator) + '\n')
                
                # 修改条件判断：只要key不为空则写入，值可为空字符串
                for index, row in df.iterrows():
                    key = row['key'].strip()
                    value = row[language_column].strip()
                    if key:  # 关键修改点
                        f.write(f'"{key}": "{value}",\n')
                
                end_separator = [
                    f'"{sheet_lower}_end_separator1": "==================================================================",',
                    f'"{sheet_lower}_end_separator2": "                                                                  ",',
                    f'"{sheet_lower}_end_separator3": "               {sheet_lower}_结束                     ",',
                    f'"{sheet_lower}_end_separator4": "                                                                  ",',
                    f'"{sheet_lower}_end_separator5": "==================================================================",'
                ]
                f.write('\n'.join(end_separator) + '\n\n')
                
            except Exception as e:
                print(f"处理Sheet [{sheet}] 时出错: {e}")
                continue
    
    print(f"语言文件 {filename} 生成完成！")

for lang_col, lang_suffix in language_map.items():
    generate_single_language_file(lang_col, lang_suffix)